<?php

$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

/* Include TUGGO system configuration file */
include("tuggo.inc.php");

if(INSTALLED == true) {

################################################################################
#+============================================================================+#
#+ Powered by TUGGO Open Source Content Mangement System (www.TUGGO.org)	  +#
#+ Code in this package is subject to the liscence in the root directory.	  +#
#+============================================================================+#
#+ TUGGO (beta) 1.0  |  Developed by Paul Cheek (pcheek@tuggo.org)			  +#
#+============================================================================+#
################################################################################

## *****************************************************************************
################################################################################
####################  (START) TUGGO Standard Configuration  ####################
################################################################################
## *****************************************************************************

/* Connect to the database using variables defined above */
$connect = mysql_connect(MYSQLHOST, MYSQLUSERNAME, MYSQLPASSWORD) 
	or die('
	
	<body style="background: #FFFFFF;">
	<style type="text/css"> @import url(admin/loginstyle.css); </style>
	<div id="tuggoerror">
		<table class="ii" cellpadding="0" cellspacing="0">
		<tr><td class="iileft" style="width: 150px;">
			<a href="<?php echo SYSURL; ?>"><img src="admin/tuggo2.png" style="width: 150px;" alt="" /></a>
		</td><td class="iiright">
			<b>TUGGO ERROR</b><br/>
			The system could not connect to MySQL!
		</td></tr>
		</table>
	</div>
	</body>
	
	');

/* Select the database after connecting to the database */
$selected = mysql_select_db(MYSQLDATABASE, $connect) 
	or die('
	
	<body style="background: #FFFFFF;">
	<style type="text/css"> @import url(admin/loginstyle.css); </style>
	<div id="tuggoerror">
		<table class="ii" cellpadding="0" cellspacing="0">
		<tr><td class="iileft" style="width: 150px;">
			<a href="<?php echo SYSURL; ?>"><img src="admin/tuggo2.png" style="width: 150px;" alt="" /></a>
		</td><td class="iiright">
			<b>TUGGO ERROR</b><br/>
			The currently defined database could not be selected!
		</td></tr>
		</table>
	</div>
	</body>
	
	');


/* Function to retrieve data from the config table of your database */
function info($id) {
$query = "SELECT * FROM config WHERE id='$id'";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
	$sqlcount++;
echo $row['value'];
mysql_free_result($result);
}

## *****************************************************************************
################################################################################
#####################  (END) TUGGO Standard Configuration  #####################
################################################################################
## *****************************************************************************

/* Retrieves the navigation menu */
function get_nav() {
 $query = "SELECT * FROM config WHERE id='5'";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);
	$online = $row['value'];
	if($online == "1") {
	
$query = "SELECT * FROM nav ORDER BY id ASC";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo "<li><a href='" . $row['link'] . "'>" . $row['name'] . "</a></li>";
}
mysql_free_result($result);
}
}

/* Fetch any variables that may be in the URL */
$function =  mysql_real_escape_string($_GET["function"]);
$id =  mysql_real_escape_string($_GET["id"]);

function tuggo_pagetitle() {

	global $function;
	
	if(isset($function)) {

		if($function == "pagewww") {
			$quer2y = "SELECT * FROM pages WHERE wwwpath='" . $_GET['id'] . "'";
			$resul2t = mysql_query($quer2y) or die(mysql_error());
			$seeif = mysql_num_rows($result);
			if($seeif == "0") {
			$ro2w = mysql_fetch_array($resul2t);
			$title = $ro2w['title'];
			echo info(1) . " &raquo; " . $title;
			} else {
			echo info(1) . " &raquo; Error";
			}
		}
		
		if($function == "page") {
			$quer3y = "SELECT * FROM pages WHERE id = '" . $_GET['id'] . "';";
			$resul3t = mysql_query($quer3y) or die(mysql_error());
			$seeif = mysql_num_rows($result);
			if($seeif == "0") {
			$ro3w = mysql_fetch_array($resul3t);
			echo info(1) . " &raquo; " . $ro3w['title'];
			} else {
			echo info(1) . " &raquo; Error";
			}
		}
		
		if($function == "blogitem") {
			$quer4y = "SELECT * FROM blog WHERE id = '" . $_GET['id'] . "';";
			$resul4t = mysql_query($quer4y) or die(mysql_error());
			$seeif = mysql_num_rows($result);
			if($seeif == "0") {
			$ro4w = mysql_fetch_array($resul4t);
			echo info(1) . " &raquo; Blog &raquo; " . $ro4w['title'];
			} else {
			echo info(1) . " &raquo; Error";
			}
		}
		
		if($function == "blog") {
			echo info(1) . " &raquo; Blog";
		}
		
		if($function == "") {
			echo info(1);
		}
		
	} else {
	
		echo info(1);
	
	}
	
}

echo "<!-- Powered By TUGGO Open Source Content Management System (www.TUGGO.org) -->";

/* Include the template files to display the content */
include("templates/header.php");

if(isset($function)) {

	if($function == "page") { // If a page is requested...
 		if(isset($id)) {
 			$query = "SELECT * FROM pages WHERE id=$id";
			$result = mysql_query($query) or die(mysql_error());
			$row = mysql_fetch_array($result);

			$id = $row['id'];
			$title = $row['title'];
			$content = $row['content'];
			$lastupdate = $row['lastupdate'];
			
			
			function tuggo_id() {
			global $id;
			echo $id;
			}
			
			function tuggo_title() {
			global $title;
			echo $title;
			}
			
			function tuggo_content() {
			global $content;
			echo $content;
			}
			
			function tuggo_lastupdate() {
			global $lastupdate;
			echo $lastupdate;
			}
			
			include("templates/page.php");
		} else {
			error();
		}
	} elseif($function == "pagewww") { // If a page is requested...
 		if(isset($id)) {
 			$query = "SELECT * FROM pages WHERE wwwpath='" . $id . "'";
			$result = mysql_query($query) or die(mysql_error());
			$row = mysql_fetch_array($result);
			
			$id = $row['id'];
			$title = $row['title'];
			$content = $row['content'];
			$lastupdate = $row['lastupdate'];
			
			
			function tuggo_id() {
			global $id;
			echo $id;
			}
			
			function tuggo_title() {
			global $title;
			echo $title;
			}
			
			function tuggo_content() {
			global $content;
			echo $content;
			}
			
			function tuggo_lastupdate() {
			global $lastupdate;
			echo $lastupdate;
			}
			
			include("templates/page.php");
		} else {
			error();
		}
	} elseif($function == "blog") { // If the blog is requested...
	
		$query = "SELECT * FROM blog WHERE status='yes' ORDER BY id DESC";
		$result = mysql_query($query) or die(mysql_error());
		$num_rows = mysql_num_rows($result);
	
		if(isset($_GET['start'])) {
		$start = $_GET['start'];
			$num = $num_rows-1;
			if($start <= $num) {
			$next = $start+10;
			} else {
			$next = "X";
			}
		} else {
		$start = "0";
		$next = $start+10;
		}
		
		if($start == "0") {
		$last = "X";
		} elseif($start <= "10") {
		$last = "0";
		} else {
		$last = $start-10;
		}
	
			$query = "SELECT * FROM blog WHERE status='yes' ORDER BY id DESC LIMIT " . $start . ", 10;";
			$result = mysql_query($query) or die(mysql_error());
			
			while($row = mysql_fetch_array($result)) {
			
				$authorid = $row['author'];
			
				$query22 = "SELECT * FROM users WHERE id='" . $authorid . "' ORDER BY id DESC";
				$result22 = mysql_query($query22) or die(mysql_error());
				$row2 = mysql_fetch_array($result22);
				
				$row['author'] = $row2['displayname'];
			
				include("templates/blog.php");
			
			}
			
			if($next == "X") {
			echo "&nbsp;";
			} else {
			echo "<a href='index.php?function=blog&start=" . $next . "'>Next Page</a>";
			}
			
			if($last == "X") {
			echo "";
			} else {
			echo "<a href='index.php?function=blog&start=" . $last . "'>Previous Page</a>";
			}
			
	} elseif($function == "blogitem") { // If the blog is requested...
		$query = "SELECT * FROM blog WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);
		
			$id = $row['id'];
			$title = $row['title'];
			$category = $row['category'];
			$author = $row['author'];
			$content = $row['content'];
			$lastupdate = $row['lastupdate'];
			
			function tuggo_id() {
			global $id;
			echo $id;
			}
			
			function tuggo_title() {
			global $title;
			echo $title;
			}
			
			function tuggo_category() {
			global $category;
			echo $category;
			}
			
			function tuggo_author() {
			global $author;
			echo $author;
			}
			
			function tuggo_lastupdate() {
			global $lastupdate;
			echo $lastupdate;
			}
			
			function tuggo_content() {
			global $content;
			echo $content;
			}
			
			function tuggo_link() {
			$query = "SELECT * FROM config WHERE id='2'";
			$result = mysql_query($query) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$sqlcount++;
			global $id;
			echo $row['value'] . "/blog/article/" . $id . "/";
			mysql_free_result($result);
			}
				
				include("templates/article.php");
				
	} elseif($function == "account") { // If an account is requested...
		$query = "SELECT * FROM users WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		
		$row = mysql_fetch_array($result);
		
			$id = $row['id'];
			$username = $row['username'];
			$displayname = $row['displayname'];
			$nickname = $row['nickname'];
			$about = $row['about'];
			$aim = $row['aim'];
			$msn = $row['msn'];
			$yim = $row['yim'];
			$gtalk = $row['gtalk'];
			$url = $row['url'];

			function tuggo_id() {
			global $id;
			echo $id;
			}
			
			function tuggo_username() {
			global $username;
			echo $username;
			}
			
			function tuggo_displayname() {
			global $displayname;
			echo $displayname;
			}
			
			function tuggo_nickname() {
			global $nickname;
			echo $nickname;
			}
			
			function tuggo_about() {
			global $about;
			echo $about;
			}
			
			function tuggo_aim() {
			global $aim;
			echo $aim;
			}
			
			function tuggo_msn() {
			global $msn;
			echo $msn;
			}
			
			function tuggo_yim() {
			global $yim;
			echo $yim;
			}
			
			function tuggo_gtalk() {
			global $gtalk;
			echo $gtalk;
			}
			
			function tuggo_url() {
			global $url;
			echo $url;
			}
			
			function tuggo_link() {
			$query = "SELECT * FROM config WHERE id='2'";
			$result = mysql_query($query) or die(mysql_error());
			$row = mysql_fetch_array($result);
			$sqlcount++;
			global $id;
			echo $row['value'] . "/blog/article/" . $id . "/";
			mysql_free_result($result);
			}
			
			include("templates/account.php");

	} else {
	$query = "SELECT * FROM config WHERE id='5'";
	$result = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_array($result);
	$online = $row['value'];
	if($online == "1") {
		$query = "SELECT * FROM pages WHERE title='Home'";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);
			
			$id = $row['id'];
			$title = $row['title'];
			$content = $row['content'];
			$lastupdate = $row['lastupdate'];
			
			
			function tuggo_id() {
			global $id;
			echo $id;
			}
			
			function tuggo_title() {
			global $title;
			echo $title;
			}
			
			function tuggo_content() {
			global $content;
			echo $content;
			}
			
			function tuggo_lastupdate() {
			global $lastupdate;
			echo $lastupdate;
			}
			
			include("templates/page.php");
	} else { 
		$query = "SELECT * FROM config WHERE id='6'";
		$result = mysql_query($query) or die(mysql_error());
		$row = mysql_fetch_array($result);
		
		$offlinemessage = $row['value'];
		
		function tuggo_offlinemessage() {
		global $offlinemessage;
		echo $offlinemessage;
		}
			
		include("templates/offline.php");
	}
	}
} else { // If no function is requested...
	error();
}

include("templates/footer.php");

/* Close MySQL connection that is currently open */
mysql_close($connect);

} else { ?>

	<body style="background: #FFFFFF;">
	<style type="text/css"> @import url(admin/loginstyle.css); </style>
	<div id="tuggoerror">
		<table class="ii" cellpadding="0" cellspacing="0">
		<tr><td class="iileft" style="width: 150px;">
			<a href="<?php echo SYSURL; ?>"><img src="admin/tuggo2.png" style="width: 150px;" alt="" /></a>
		</td><td class="iiright">
			<b>TUGGO ERROR</b><br/>
			The system has not yet been installed, would you like to <a href="./install/">install it now</a>?
		</td></tr>
		</table>
	</div>
	</body>

 <?php } ?>

<?php
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);

echo "<!-- TUGGO served up this page in " . substr($totaltime, 0, 5) . " seconds...impressive, eh?
Want some of this servage?  Get the TUGGO Website Management System for FREE today!  (www.TUGGO.org) -->";
?>